import { Texture, Sprite, Text, Container, Graphics, TextStyle } from "pixi.js";

export const patchProp = (
    el,
    key,
    prevValue,
    nextValue,
    isSVG = false,
    prevChildren,
    parentComponent,
    parentSuspense,
    unmountChildren
) => {
    switch (key) {
        case "pos":
            el.moveTo(nextValue.x, nextValue.y);
            break;
        case "texture":
            el.texture = Texture.from(nextValue);
            break;
        case "onClick":
            el.on("pointertap", nextValue);
            break;
        case "rectstyle":
            let { color: rectColor, alpha: rectAlpha } = nextValue
            // el.beginFill(rectColor, rectAlpha); //0x66ccff
            // el.drawRect(0, 0, 1, 1);
            // el.endFill();

            if (el.rect) {
                el.removeChild(el.rect);
            }
            el.rect = new Graphics();
            el.rect.beginFill(rectColor, rectAlpha); //0x66ccff
            el.rect.drawRect(0, 0, 1, 1);
            el.rect.endFill();
            el.addChild(el.rect);
            break;
        case "circlestyle":
            let { color: circleColor, alpha: circleAlpha, linewidth: circleLineWidth, radius } = nextValue;
            //方式1：子元素方式
            if (el.circle) {
                el.removeChild(el.circle);
            }
            el.circle = new Graphics();
            el.circle.lineStyle(circleLineWidth, circleColor, circleAlpha);
            el.circle.drawCircle(0, 0, radius);
            el.addChild(el.circle);
            //方式2：直接方式  这种方式特别卡，暂没找到原因
            // el.beginFill(circleColor, 1); //0x66ccff
            // el.lineStyle(circleLineWidth, circleColor, circleAlpha);
            // el.drawCircle(0, 0, 1);
            // // el.drawRect(0, 0, 1, 1);
            // el.endFill();
            break;
            // case "text":
            //     el.text = nextValue;
            //     break;
        case "textsytle":
            let { color: textColor, fontSize: fontSize, align: align = "center", textContent: textContent } = nextValue;
            // if (el.__text) {
            //     el.removeChild(el.__text);
            // }
            // el.__text = new Text(textContent, { fontFamily: 'Arial', fontSize: fontSize, fill: textColor, align: 'center' });
            // el.addChild(el.__text);
            // console.log('el.text', el.text); //sy-log
            el.style = new TextStyle({ fontFamily: 'Arial', fontSize: fontSize, fill: textColor, align: align });
            break
        case "anchor": //定位起点
            const { x: anchorx = 0, y: anchory = 0 } = nextValue
            el.anchor.x = anchorx;
            el.anchor.y = anchory;
            break;
        case "pivot":
            const { x: pivotx, y: pivoty } = nextValue;
            if (pivotx || pivot == 0) {
                el.pivot.x = pivotx
            }
            if (pivoty || pivoty == 0) {
                el.pivot.y = pivoty
            }
            break;
        default:
            // console.log(nextValue, key);
            el[key] = nextValue;
            break;
    }
}